package com.example.micro_property_springboot.controller;

import com.example.micro_property_springboot.pojo.AssetCheckPlan;
import com.example.micro_property_springboot.pojo.AssetCheckItem;
import com.example.micro_property_springboot.pojo.AssetCheckDiff;
import com.example.micro_property_springboot.pojo.DTO.request.AssetCheckPlanCreateRequest;
import com.example.micro_property_springboot.pojo.DTO.request.AssetCheckPlanUpdateRequest;
import com.example.micro_property_springboot.pojo.DTO.request.AssetCheckItemRequest;
import com.example.micro_property_springboot.pojo.DTO.request.AssetCheckDiffHandleRequest;
import com.example.micro_property_springboot.pojo.DTO.request.AssetCheckPlanResponse;
import com.example.micro_property_springboot.service.AssetCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 资产盘点控制器
 * 提供盘点计划管理、盘点执行和统计分析的REST API
 */
@RestController
@RequestMapping("/api/asset/check")
public class AssetCheckController {

    @Autowired
    private AssetCheckService assetCheckService;

    // ========== 盘点计划管理 ==========
    /**
     * 创建盘点计划
     * @param request 盘点计划创建请求
     * @return 创建的盘点计划
     */
    @PostMapping("/plan")
    public AssetCheckPlan createCheckPlan(@RequestBody AssetCheckPlanCreateRequest request) {
        return assetCheckService.createCheckPlan(request);
    }

    /**
     * 更新盘点计划
     * @param planId 计划ID
     * @param request 盘点计划更新请求
     * @return 更新后的盘点计划
     */
    @PutMapping("/plan/{planId}")
    public AssetCheckPlan updateCheckPlan(@PathVariable Long planId, @RequestBody AssetCheckPlanUpdateRequest request) {
        return assetCheckService.updateCheckPlan(planId, request);
    }

    /**
     * 获取盘点计划详情
     * @param planId 计划ID
     * @return 盘点计划响应
     */
    @GetMapping("/plan/{planId}")
    public AssetCheckPlanResponse getCheckPlanDetail(@PathVariable Long planId) {
        return assetCheckService.getCheckPlanDetail(planId);
    }

    /**
     * 获取盘点计划列表
     * @param params 查询参数
     * @return 盘点计划列表
     */
    @GetMapping("/plan/list")
    public List<AssetCheckPlanResponse> getCheckPlanList(@RequestParam Map<String, Object> params) {
        return assetCheckService.getCheckPlanList(params);
    }

    /**
     * 删除盘点计划
     * @param planId 计划ID
     * @return 是否删除成功
     */
    @DeleteMapping("/plan/{planId}")
    public boolean deleteCheckPlan(@PathVariable Long planId) {
        return assetCheckService.deleteCheckPlan(planId);
    }

    // ========== 盘点执行 ==========
    /**
     * 开始盘点计划
     * @param planId 计划ID
     * @return 是否开始成功
     */
    @PutMapping("/plan/{planId}/start")
    public boolean startCheckPlan(@PathVariable Long planId) {
        return assetCheckService.startCheckPlan(planId);
    }

    /**
     * 结束盘点计划
     * @param planId 计划ID
     * @return 是否结束成功
     */
    @PutMapping("/plan/{planId}/finish")
    public boolean finishCheckPlan(@PathVariable Long planId) {
        return assetCheckService.finishCheckPlan(planId);
    }

    /**
     * 标记资产已盘点
     * @param request 盘点明细操作请求
     * @return 是否操作成功
     */
    @PostMapping("/item/check")
    public boolean markAssetChecked(@RequestBody AssetCheckItemRequest request) {
        return assetCheckService.markAssetChecked(request);
    }

    /**
     * 获取盘点明细列表
     * @param planId 计划ID
     * @return 盘点明细列表
     */
    @GetMapping("/items")
    public List<AssetCheckItem> getCheckItems(@RequestParam Long planId) {
        return assetCheckService.getCheckItems(planId);
    }

    // ========== 盘点差异处理 ==========
    /**
     * 获取盘点差异列表
     * @param planId 计划ID
     * @return 盘点差异列表
     */
    @GetMapping("/diffs")
    public List<AssetCheckDiff> getCheckDiffs(@RequestParam Long planId) {
        return assetCheckService.getCheckDiffs(planId);
    }

    /**
     * 处理盘点差异
     * @param diffId 差异ID
     * @param request 差异处理请求
     * @return 是否处理成功
     */
    @PutMapping("/diff/{diffId}")
    public boolean handleCheckDiff(@PathVariable Long diffId, @RequestBody AssetCheckDiffHandleRequest request) {
        return assetCheckService.handleCheckDiff(diffId, request);
    }

    // ========== 盘点报表和统计 ==========
    /**
     * 获取盘点记录报表
     * @param params 查询参数
     * @return 盘点记录报表数据
     */
    @GetMapping("/report/record")
    public List<Map<String, Object>> getCheckRecordReport(@RequestParam Map<String, Object> params) {
        return assetCheckService.getCheckRecordReport(params);
    }

    /**
     * 获取盘点统计分析数据
     * @param planId 计划ID
     * @return 盘点统计分析数据
     */
    @GetMapping("/statistics/{planId}")
    public Map<String, Object> getCheckStatistics(@PathVariable Long planId) {
        return assetCheckService.getCheckStatistics(planId);
    }

    /**
     * 获取部门盘点情况统计
     * @return 部门盘点情况统计
     */
    @GetMapping("/statistics/dept")
    public List<Map<String, Object>> getDeptCheckStatistics() {
        return assetCheckService.getDeptCheckStatistics();
    }

    /**
     * 获取盘点完成率趋势
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 盘点完成率趋势数据
     */
    @GetMapping("/statistics/trend")
    public List<Map<String, Object>> getCheckCompletionRateTrend(
            @RequestParam String startDate,
            @RequestParam String endDate) {
        return assetCheckService.getCheckCompletionRateTrend(startDate, endDate);
    }
}